<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateGoodsRequest;
use App\Http\Requests\UpdateGoodsRequest;
use App\Models\Goods;
use App\Repositories\GoodsRepository;
use App\Http\Controllers\AppBaseController;
use App\Repositories\PicturesRepository;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class GoodsController extends AppBaseController
{
    /** @var  GoodsRepository */
    private $goodsRepository;
    private $pic;

    public function __construct(GoodsRepository $goodsRepo, PicturesRepository $picturesRepository)
    {
        $this->goodsRepository = $goodsRepo;
        $this->pic = $picturesRepository;
    }

    /**
     * Display a listing of the Goods.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->goodsRepository->pushCriteria(new RequestCriteria($request));
        $goods = $this->goodsRepository->paginate(20);

        return view('goods.index')
            ->with('goods', $goods);
    }

    /**
     * Show the form for creating a new Goods.
     *
     * @return Response
     */
    public function create()
    {
        return view('goods.create');
    }

    /**
     * Store a newly created Goods in storage.
     *
     * @param CreateGoodsRequest $request
     *
     * @return Response
     */
    public function store(CreateGoodsRequest $request)
    {
        $input = $request->all();
        $_image = $request->file('image');
        $_res = $this->pic->uploadByHash($_image,"goods_content",null,null);
        $input['image'] = $_res->url;
        $goods = $this->goodsRepository->create($input);

        Flash::success('Goods saved successfully.');

        return redirect(route('goods.index'));
    }

    /**
     * Display the specified Goods.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $goods = $this->goodsRepository->findWithoutFail($id);

        if (empty($goods)) {
            Flash::error('Goods not found');

            return redirect(route('goods.index'));
        }

        return view('goods.show')->with('goods', $goods);
    }

    /**
     * Show the form for editing the specified Goods.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $goods = $this->goodsRepository->findWithoutFail($id);

        if (empty($goods)) {
            Flash::error('Goods not found');

            return redirect(route('goods.index'));
        }

        return view('goods.edit')->with('goods', $goods);
    }

    /**
     * Update the specified Goods in storage.
     *
     * @param  int              $id
     * @param UpdateGoodsRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateGoodsRequest $request)
    {
        $goods = $this->goodsRepository->findWithoutFail($id);

        if (empty($goods)) {
            Flash::error('Goods not found');

            return redirect(route('goods.index'));
        }
        $input = $request->all();
        $_image = $request->file('image');
        $_res = $this->pic->uploadByHash($_image,"goods_content",null,null);
        $input['image'] = $_res->url;

        $goods = $this->goodsRepository->update($input, $id);

        Flash::success('Goods updated successfully.');

        return redirect(route('goods.index'));
    }

    /**
     * Remove the specified Goods from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $goods = $this->goodsRepository->findWithoutFail($id);

        if (empty($goods)) {
            Flash::error('Goods not found');

            return redirect(route('goods.index'));
        }

        $this->goodsRepository->delete($id);

        Flash::success('Goods deleted successfully.');

        return redirect(route('goods.index'));
    }

    public function stock(Request $request)
    {
        $input = $request->all();
        $goods = Goods::find($input['goods_id']);
        $goods->average_price = ($goods->number*$goods->average_price+$input['number']*$input['money'])/($goods->number + $input['number']);
        $goods->number += $input['number'];
        $goods->rate = ($goods->price - $goods->discounts - $goods->average_price)/$goods->average_price;
        $goods->save();
        Flash::success('Goods stock successfully.');

        return redirect(route('goods.index'));
    }
}
